package com.web.a.weba;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.alibaba.fastjson.JSONObject;
import com.web.a.weba.filter.TimeRankFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

@SpringBootApplication
@EnableDubboConfiguration
public class WebaApplication {

    public static void main(String[] args) {

        SpringApplication.run(WebaApplication.class, args);

        Thread threadTwo=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                     Map<String, TreeMap<Long, Long>> timeRecoredMapTree = TimeRankFilter.timeRecoredMapTree;

                    for (Map.Entry<String, TreeMap<Long, Long>> stringTreeMapEntry : timeRecoredMapTree.entrySet()) {
                         String key = stringTreeMapEntry.getKey();
                         TreeMap<Long, Long> value = stringTreeMapEntry.getValue();
                        Long total=value.values().stream().mapToLong(o->o).sum();
                        TreeMap<Long, Long> transMap = new TreeMap<>(Comparator.naturalOrder());
                        AtomicReference<Long> idx= new AtomicReference<>(0l);
                        value.forEach((keys,values)->{
                            transMap.put(idx.get(),keys);
                            idx.updateAndGet(v -> v + values);
                        });
                        Double idx90= Math.ceil((double)total*90/100);
                        Double idx99= Math.ceil((double)total*99/100);
                        Map.Entry<Long, Long> idx90Entry = transMap.floorEntry(idx90.longValue());
                        Map.Entry<Long, Long> idx99Entry = transMap.floorEntry(idx99.longValue());
                        Long TP90=idx90Entry.getValue();
                        Long TP99=idx99Entry.getValue();
                        System.out.println("========"+key+":TP90:"+TP90+"===TP99:"+TP99);
                    }
                }
            }
        });

        threadTwo.start();

    }

}
